package com.annoncesautomobile.vue.autres;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.annoncesautomobile.R;
import com.annoncesautomobile.modele.concreteEntities.DetailAnnonce;
import com.annoncesautomobile.modele.local.MesAnnoncesAlertes;
import com.annoncesautomobile.modele.local.MesFavoris;
import com.annoncesautomobile.modele.util.IModelXML;
import com.annoncesautomobile.modele.util.ModelXML;
import com.annoncesautomobile.vue.SuperActivity;
import com.annoncesautomobile.vue.adapters.ListeBoutonBas;
import com.annoncesautomobile.vue.affichageannonces.AffichageDetailAnnonceActivity;

public class MesFavorisActivity extends SuperActivity {

	private final String EXTRA_IDANNONCE = "idAnnonce";

	private ProgressDialog progressDialog;

	private IModelXML modele;
	private Set<Long> storedFavoris;
	private List<Long> listIdAnnonces;
	private Map<Long, DetailAnnonce> mapAnnonces;
	private boolean modification;

	private ListView listViewAnnonces;
	private ListViewMesFavorisAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.activity_mes_favoris);

		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar);
		TextView textViewActivityTitleText = (TextView) findViewById(R.id.activity_title_text);
		textViewActivityTitleText.setText(getString(R.string.title_activity_mes_favoris));

		ListeBoutonBas listeBoutonBas = new ListeBoutonBas(this);
		LinearLayout lt = (LinearLayout) this.findViewById(R.id.mainLayoutMesFavoris);
		lt.addView(listeBoutonBas.getView());

		this.modele = ModelXML.getInstance();

		this.listIdAnnonces = new CopyOnWriteArrayList<Long>();
		this.mapAnnonces = new HashMap<Long, DetailAnnonce>();

		this.adapter = new ListViewMesFavorisAdapter(MesFavorisActivity.this, this.listIdAnnonces, this.mapAnnonces);

		this.progressDialog = new ProgressDialog(this);
		this.progressDialog.setMessage("Chargement de vos favoris...");

		this.listViewAnnonces = (ListView) this.findViewById(R.id.listViewMesFavoris);
		this.listViewAnnonces.setAdapter(this.adapter);	
		this.listViewAnnonces.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// Lancement Detail annonce
				Intent intent = new Intent(MesFavorisActivity.this, AffichageDetailAnnonceActivity.class);
				intent.putExtra(EXTRA_IDANNONCE, id);
				startActivity(intent);

			}
		});
		this.listViewAnnonces.setEmptyView(findViewById(R.id.empty));
	}

	public void onBackPressed(View view) {
		this.onBackPressed();
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}

	@Override
	protected void onNewIntent(Intent newIntent) {
		this.setIntent(newIntent);
	}

	@Override
	protected void onResume() {
		super.onResume();
		this.loadContent();
	}

	protected synchronized void loadContent(){
		this.storedFavoris = MesFavoris.loadMesFavoris(getApplicationContext());
		this.loadMoreData();

	}

	protected synchronized void loadMoreData() {
		new LoadDataAnnonces().execute();
	}

	protected void displayContent() {
		this.adapter.notifyDataSetChanged();
		if(!this.listIdAnnonces.isEmpty()) {
			this.listViewAnnonces.setSelection(0);
		}
	}

	private class LoadDataAnnonces extends AsyncTask<Void, Void, Void> {

		@Override
		protected void onPreExecute() {
			if(!progressDialog.isShowing()) {
				progressDialog.show();
			}
		}

		@Override
		protected Void doInBackground(Void... params) {
			synchronized (MesFavorisActivity.this) {

				modification = false;
				for(Long idAnnonce : listIdAnnonces) {
					if(!storedFavoris.contains(idAnnonce)) {
						listIdAnnonces.remove(idAnnonce);
						mapAnnonces.remove(idAnnonce);
						modification = true;
					}
				}
				for(Long idAnnonce : storedFavoris) {
					if(!listIdAnnonces.contains(idAnnonce)) {
						DetailAnnonce annonce = modele.getDetailAnnonce(idAnnonce.intValue());
						if(annonce == null) {
							MesFavoris.setUnsetFavori(getApplicationContext(), idAnnonce);
						} else {
							listIdAnnonces.add(idAnnonce);
							mapAnnonces.put(idAnnonce, annonce);
						}
						modification = true;
					}
				}
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void p) {
			if(progressDialog.isShowing()) {
				progressDialog.dismiss();
			}
			if(modification) {
				MesFavorisActivity.this.displayContent();
			}
		}
	}

}
